package com.hyz.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hyz.dao.UserMapper;
import com.hyz.exception.ExceptionCast;
import com.hyz.pojo.User;
import com.hyz.response.StatusCode;
import com.hyz.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public List<User> findUserAll() {
        return userMapper.findUserAll();
    }

    @Override
    public boolean updataUserById(User user) {
        // 参数校验
        if (user.getRealName() == null) {
            ExceptionCast.cast( StatusCode.USER_DATAPARAM_INVALID_ERROR );
        }
        return userMapper.updataUserById(user);
    }

    @Override
    public User add(User user) {
        // 参数校验
        if (user.getRealName() == null) {
            ExceptionCast.cast( StatusCode.USER_DATAPARAM_INVALID_ERROR );
        }
         userMapper.add(user);
         return user;
    }

    @Override
    public boolean del(Integer id) {
        // 参数校验
        if (id == null) {
            ExceptionCast.cast( StatusCode.USER_DATAPARAM_INVALID_ERROR );
        }
        return userMapper.del(id);
    }

    @Override
    public Page<User> findPageUser(User user, Integer page, Integer size) {
        System.out.println("user = " + user);
        System.out.println(user.getUserName());
        //分页
        PageHelper.startPage(page,size);
        //条件查询
        Page<User> pageList =
                (Page<User>)userMapper.findPageUser(user);
        return pageList;
    }
}
